package org.Panel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import javafx.geometry.Pos;
import org.Component.CommentBar;
import org.Component.PostMenuBar;
import org.JsonMsg.PersonalMsg;
import org.JsonMsg.Post;
import org.JsonMsg.ReplyMsg;
import org.Tools.OnLineOP;
import org.Tools.StringOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//通用帖子内容
public class PostPanel extends FaPanel{

    //帖子标题
    String PostTitle;
    JLabel PostTitleLabel = new JLabel();
    //帖子内容
    String content;
    JLabel PostContentLabel;
    //贴主头像
    JButton HeadPortrait = new JButton("头像");
    //贴主用户名
    String username;
    JLabel usernameLabel = new JLabel("svc");
    //日期
    JLabel DateLabel = new JLabel("2012-11-30 12:00:00");
    //菜单栏
    PostMenuBar postMenuBar;
    //评论区
    CommentPanel commentPanel ;
    //滚动界面
    ScrollPanel scrollPanel ;
    //内部界面
    JPanel PostIn = new JPanel();
    //修改按钮
    JLabel UpdatePost = new JLabel("修改");
    //置顶
    JLabel TopButton = new JLabel("置顶");
    //点赞数
    JLabel likes =  new JLabel("点赞数：");
    int likesSum = 0;
    //点赞按钮
    JLabel likeButton = new JLabel("点赞");
    //母界面接口
    PanelSize panelSize;
    PanelSkip panelSkip;
    //Json操作工具
    Gson gson = new Gson();
    //高
//    int hy=10;
    //评论区的高
    int comh = 0;


    public PostPanel(PanelSize panelSize, PanelSkip panelSkip){

        this.panelSize = panelSize;
        this.panelSkip = panelSkip;
        //属性设置
        PanelIndex = 5;
        setLayout(null);
        setBounds(0,0,panelSize.getWidth(),panelSize.getHeight());

        //滚动界面
        scrollPanel = new ScrollPanel(getWidth(), getHeight()-100);
        scrollPanel.setBounds(0,100,getWidth(),getHeight()-100);

        //标题
        PostTitleLabel.setBounds(50,10,getWidth()-100,50);
        Font font = new Font(null,0,30);
        PostTitleLabel.setFont(font);
        PostTitleLabel.setText("ahhhhh");

        //内容
        PostContentLabel = new JLabel();
        PostContentLabel.setBounds(50,100,getWidth()-100,500);
        font = new Font(null,0,20);
        PostContentLabel.setFont(font);
        PostContentLabel.setText("dafdljakfjh;sakdf");

        //用户名
        usernameLabel.setBounds(400,600,100,50);


        //日期
        DateLabel.setBounds(700,600,200,50);

        //点赞数
        likes.setBounds(150,600,200,50);
        //点赞按钮
        likeButton.setBounds(10,600,80,50);
        likeButton.addMouseListener(new MouseAdapter() {
            int f =1;
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    //更新主页
                    String msg = "postlike-{\"postId\":\""+MainPanel.post.getPostId()+"\",\"userId\":\""+MainPanel.post.getUserId()+"\"}-"+MainPanel.userName;
                    System.out.println(msg);
                    //从服务器端获取的数据
                    String jsonmsg;
                    OnLineOP onLineOP = new OnLineOP();
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println(jsonmsg);
                    if(jsonmsg.equals("true")) {
                        likesSum++;
                        likes.setText("点赞数："+likesSum);
                    }

//                    panelSkip.skip(5);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
                likeButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                likeButton.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                likeButton.setForeground(Color.BLACK);
            }
        });

        //修改按钮
        UpdatePost.setBounds(920,600,80,50);
        UpdatePost.addMouseListener(new MouseAdapter() {
            int f =1;
            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1) {
                    //更新主页
                    String msg = "";
                    //从服务器端获取的数据

                    panelSkip.skip(6);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
                UpdatePost.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                UpdatePost.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
                UpdatePost.setForeground(Color.BLACK);
            }
        });

        //评论区
        commentPanel = new CommentPanel(panelSkip);
        commentPanel.setLocation(50,PostTitleLabel.getHeight()+PostContentLabel.getHeight());
        CommentBar com = new CommentBar("svc","fsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffdsfsajdlfhlkasdhfakljsdhfklajhsdsadfasdfasdfsdffds","2012-12-23 16:23",commentPanel);
        commentPanel.addCommentBar(com);

        //内部界面设置
        PostIn.setLayout(null);
        PostIn.setBounds(0,0,panelSize.getWidth(),2000);
        PostIn.add(PostTitleLabel);
        PostIn.add(PostContentLabel);
//        PostIn.add(HeadPortrait);
        PostIn.add(usernameLabel);
        PostIn.add(DateLabel);
        PostIn.add(UpdatePost);
        PostIn.add(TopButton);
        PostIn.add(likes);
        PostIn.add(likeButton);
        PostIn.add(commentPanel);
        scrollPanel.setPanel(PostIn);

        //菜单条
        postMenuBar = new PostMenuBar(panelSize, panelSkip);
        postMenuBar.setBounds(0,0,getWidth(),100);

        //添加至界面
        add(scrollPanel);
        add(postMenuBar);


    }

    public void Run() {
        scrollPanel.run();
        int hi = PostTitleLabel.getHeight()+PostContentLabel.getHeight()+usernameLabel.getHeight()+commentPanel.getHeight()+40;
        PostIn.setSize(PostIn.getWidth(),hi);
//        //计算查值评论区的高度差值，改变界面的大小
//        int dy = commentPanel.getHeight()-comh;
//        hy+=dy;
//        comh = commentPanel.getHeight();
//        PostIn.setSize(PostIn.getWidth(),hy);
//        System.out.println("PostPanel");
//        System.out.println(getWidth());
//        System.out.println(getHeight());

    }

    public void updatePanel() {
        Post post = MainPanel.post;
        String title = post.getTitle();
        String content = post.getContent();
        int postId = post.getPostId();
        int userId = post.getUserId();
//        String power;

        //向服务器发出请求的字符串
        String msg;
        //服务器返回信息
        String jsonmsg;

        //字符串操作工具
        StringOP stringOP = new StringOP();
        //网络操作工具
        OnLineOP onLineOP = new OnLineOP();
        //测量字符串宽度工具
        FontMetrics fm;
        //高
        int hy=10;
        //标题
        fm = PostTitleLabel.getFontMetrics(PostTitleLabel.getFont());
        title = stringOP.LabelStr(title,fm,800);
        PostTitleLabel.setText(title);
        PostTitleLabel.setBounds(10,hy,getWidth()-100,fm.getHeight()*stringOP.row);
        hy+=PostTitleLabel.getHeight()+10;
        //内容
        fm = PostContentLabel.getFontMetrics(PostContentLabel.getFont());
        content = stringOP.LabelStr(content,fm,800);
        PostContentLabel.setText(content);
        PostContentLabel.setBounds(10,hy,getWidth()-100,fm.getHeight()*stringOP.row);
        hy+=PostContentLabel.getHeight()+10;
        //作者,日期,修改按钮
        usernameLabel.setLocation(usernameLabel.getX(),hy);
        //获取作者信息
        msg = "getUserIdByUser-"+userId+"-svc";
        try {
            jsonmsg = onLineOP.sendone(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PersonalMsg personalMsg = new PersonalMsg();//个人信息类
        personalMsg = gson.fromJson(jsonmsg, PersonalMsg.class);
        username = personalMsg.getUsername();
        usernameLabel.setText("作者："+username);
        //日期。修改按钮
        DateLabel.setLocation(DateLabel.getX(),hy);
        DateLabel.setText(post.getUpdatedAt());
        UpdatePost.setLocation(UpdatePost.getX(),hy);
        //点赞
        likes.setText("点赞数："+post.getLikesCount());
        likesSum = post.getLikesCount();
        likes.setLocation(likes.getX(),hy);
        likeButton.setLocation(likeButton.getX(),hy);
        //如果不是作者或者管理员，则关闭修改按钮
        if(userId!=MainPanel.userId&&MainPanel.userPower.equals("ADMIN")==false) {
            UpdatePost.setSize(0,0);
        }else {
            UpdatePost.setSize(80,50);
        }
        hy+=usernameLabel.getHeight()+10;
        //评论区
        PostIn.remove(commentPanel);    //删除原来的评论区
        commentPanel = new CommentPanel(panelSkip);  //新建一个新的评论区
        msg = "getReply-{\"postId\":"+postId+"}-"+MainPanel.userName;
        try {
            jsonmsg = onLineOP.sendone(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 由于要解析为List<Post>类型，需要使用TypeToken来获取准确的类型信息
        Type listType = new TypeToken<java.util.List<ReplyMsg>>() {}.getType();
        List<ReplyMsg> replyList = gson.fromJson(jsonmsg, listType);
        for(ReplyMsg replyMsg : replyList) {
            //评论者的用户名
            msg = "getUserIdByUser-"+replyMsg.getUserId()+"-svc";
            try {
                jsonmsg = onLineOP.sendone(msg);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            PersonalMsg personalMsg2 = new PersonalMsg();//个人信息类
            personalMsg2 = gson.fromJson(jsonmsg, PersonalMsg.class);
            String uname = personalMsg2.getUsername();
            //获取日期格式
            Date date = new Date(replyMsg.getCreatedAt());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = sdf.format(date);
            //插入评论条
            CommentBar commentBar = new CommentBar(uname,replyMsg.getContent(),formattedDate,commentPanel);
            commentPanel.addCommentBar(commentBar);
        }
        commentPanel.setLocation(50,hy);
        hy+=commentPanel.getHeight()+10;
        PostIn.add(commentPanel);
//        comh = commentPanel.getHeight();    //记录评论区的高度
        //整个大小更新
        if(hy<600) hy=600;
        PostIn.setSize(PostIn.getWidth(),hy);



    }


}
